﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SARD.Aux_Forms
{
    public partial class local : Form
    {

        string currentdirectory;
        List<string> directorypaths;
        string file;
        string image;


        /// <summary>
        /// Initializes the form
        /// </summary>
        public local()
        {
            InitializeComponent();
            updatelistbox1();
            file = null;
            image = null;
            directorypaths = new List<string>();

        }


        /// <summary>
        /// Initializes the form using the image path as a parameter.
        /// </summary>
        /// <param name="image">
        /// It's the image path.
        /// </param>
        public local(string image)
        {
            InitializeComponent();
            this.image = image;
            file = null;
            updatelistbox1();
            directorypaths = new List<string>();

        }

        /// <summary>
        /// This function changes the services data displayed.
        /// </summary>
        private void updatelistbox1() 
        {
            currentdirectory = Directory.GetCurrentDirectory();
            string[] Subdir = Directory.GetDirectories(currentdirectory);


            this.listBox1.Items.Clear();
            string temp;
            int temppos;

            foreach (String str in Subdir)
            {
                temppos = str.LastIndexOf('\\');
                temp = str.Substring(temppos + 1);
                this.listBox1.Items.Add(temp);
            }
            this.listBox1.Invalidate();

        }


        /// <summary>
        /// Includes a new service
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            novolocal novo = new novolocal();
            novo.ShowDialog();
            updatelistbox1();

        }


        /// <summary>
        /// Closes the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            
            this.Close();

        }


        /// <summary>
        /// When the listbox changed it is updated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            updatelistbox2();
        }

        /// <summary>
        /// This function changes the equipment data displayed.
        /// </summary>
        private void updatelistbox2() 
        {
            string[] all = Directory.GetDirectories(Environment.CurrentDirectory);
            this.listBox2.Items.Clear();

            if (listBox1.SelectedIndex >= 0)
            {
                string[] allequipment = Directory.GetDirectories(all[listBox1.SelectedIndex]);
                //Due to the differences between CR and DR equipments it is necessary to use an auxiliar list to get the desired tests.
                directorypaths.Clear();

                foreach (String temp in allequipment)
                {
                    string temp2 = temp.Substring(temp.LastIndexOf('\\') + 1);
                    directorypaths.Add(temp);
                    this.listBox2.Items.Add(temp2);
                    if (temp2.Contains("#CR"))
                    {
                        string[] cr = Directory.GetDirectories(temp);
                        foreach (String c in cr)
                        {
                            this.listBox2.Items.Add("\t" + c.Substring(c.LastIndexOf('\\') + 1));
                            directorypaths.Add(c);
                        }
 
                    }
                }

                this.listBox2.Invalidate();
            }

        }

        /// <summary>
        /// This function changes the test data displayed.
        /// </summary>
        private void updatelistbox3() 
        {

            this.listBox3.Items.Clear();

            if (listBox1.SelectedIndex >= 0)
            {
                
                if (listBox2.SelectedIndex >= 0)
                {
                    string[] alltests = Directory.GetDirectories(directorypaths[listBox2.SelectedIndex]);
                    foreach (String temp in alltests)
                    {
                        string[] temp2 = Directory.GetFiles(temp, "*.txt");
                        this.listBox3.Items.Add(temp2[0].Substring(temp2[0].LastIndexOf('\\') + 1));
                    }


                    this.listBox3.Invalidate();
                }
            }

        }

        /// <summary>
        /// Causes invalidation of the test data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            updatelistbox3();
        }

        /// <summary>
        /// Insert a new equipment.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                
                string[] all = Directory.GetDirectories(Environment.CurrentDirectory);
                equipamentonovo novo = new equipamentonovo(all[listBox1.SelectedIndex], true);
                novo.ShowDialog();
                updatelistbox2();
            }
        }


        /// <summary>
        /// Includes a new test.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0 && listBox2.SelectedIndex >=0)
            {

                analise analise = new analise(directorypaths[listBox2.SelectedIndex], image,  true);
                analise.ShowDialog();
                updatelistbox3();
            }
        }


        /// <summary>
        /// Changes the service data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                string[] all = Directory.GetDirectories(Environment.CurrentDirectory);
                novolocal novo = new novolocal(all[listBox1.SelectedIndex], false);
                novo.ShowDialog();
                updatelistbox1();
            }
        }


        /// <summary>
        /// Changes the equipments data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0 && listBox2.SelectedIndex >= 0)
            {

                if (!directorypaths[listBox2.SelectedIndex].Contains("\t"))
                {
                    equipamentonovo novo = new equipamentonovo(directorypaths[listBox2.SelectedIndex], false);
                    novo.ShowDialog();
                }
                updatelistbox2();
            }
        }


        /// <summary>
        /// Changes the test data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0 && listBox2.SelectedIndex >= 0 && listBox3.SelectedIndex >= 0)
            {

                string[] alltests = Directory.GetDirectories(directorypaths[listBox2.SelectedIndex]);
                analise analise = new analise(alltests[listBox3.SelectedIndex], image,  false);
                analise.ShowDialog();
                updatelistbox3();
            }
        }

        /// <summary>
        /// Returns the file path of a test.
        /// </summary>
        /// <returns>
        /// Returns the string of the file path.
        /// </returns>
        public string getfile()
        {
            if (listBox2.SelectedIndex >= 0 && listBox3.SelectedIndex >= 0)
            {
                string[] alltests = Directory.GetDirectories(directorypaths[listBox2.SelectedIndex]);
                string[] files = Directory.GetFiles(alltests[listBox3.SelectedIndex], "*.txt");
                file = files[0];
                return file;

            }
            else
                return null;

        }
    }
}
